package com.pt25.base.model.security.aspect;


import com.pt25.base.exception.BizException;
import com.pt25.base.model.security.UserSession;
import com.pt25.base.res.ResCode;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Set;


/**
 * LoginCheckAspect（登录验证）
 * RoleCheckAspect（角色验证）
 */
@Slf4j
@Order(3)
@Component
@Aspect
class RoleCheckAspect extends BaseAspect {

    @Before("@annotation(com.pt25.base.model.security.aspect.RoleCheck)")
    public void check(JoinPoint joinPoint) {
        UserSession<?> userSession = securityService.getSessionData();
        if(null != userSession) {
            Set<String> roleSet = userSession.getRoleList();
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            RoleCheck roleCheck = signature.getMethod().getAnnotation(RoleCheck.class);
            check(userSession.getRoleList(), roleCheck.value(), roleCheck.and(), roleSet);
        }else{
            throw new BizException(ResCode.UN_LOGIN.getCode(), "用户未登录！");
        }
    }
}
